package jvm;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import org.omg.PortableInterceptor.Interceptor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OOMdemo {

  // -XX:+PrintGC -Xmx=8M -Xms=8M  -Xloggc:/Users/ycy/Desktop/linshi/gc.log
  //  -XX:+PrintGCDetails -Xmx=8M -Xms=8M  -Xloggc:/Users/ycy/Desktop/linshi/gc.log
//  public static void main(String[] args) {
//    test1();
//  }

  //   -XX:MaxMetaspaceSize=1m
  public static void main(String[] args) {

    test2();
  }

  public static void test1() {
    int i = 0;
    try {
//      for (int j = 0; j < 1000; j++) {
//        byte[] arr = new byte[1024*1000];
//      }
      byte[] arr1 = new byte[1024 * 1000];
      byte[] arr2 = new byte[1024 * 1000];
      byte[] arr3 = new byte[1024 * 1000];
//      byte[] arr4 = new byte[1024*1000*8];
      while (true) {
        System.out.println(".");
        Thread.sleep(10);
        byte[] arr4 = new byte[1024 * 1000];
      }
    } catch (Throwable e) {
      e.printStackTrace();
      System.out.println("结束");
    }

  }

  public static void test2() {

    Map<String,String> map = new HashMap<>();

    List<Object> list = new ArrayList<Object>();

    for (int i = 0;  ;  ) {
      Enhancer enhancer = new Enhancer();
      enhancer.setSuperclass(OOMdemo.class);

      enhancer.setCallback(new MethodInterceptor() {
        @Override
        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
          proxy.invoke(obj,args);
          return null;
        }
      });

      Object o = enhancer.create();
      list.add(o);

    }

  }


}
